package com.mvetc.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.mvetc.dao.MoviesDao;
import com.mvetc.entity.Country;
import com.mvetc.entity.Movies;
import com.mvetc.entity.PageInfo;
import com.mvetc.util.DBUtil;

public class MoviesDaoImpl implements MoviesDao{

	@Override
	public PageInfo<Movies> getListMoviesByConditionBycf(Map<String, String> params) {
		List<Movies> list=new ArrayList<Movies>();
		ResultSet rs=null;
		Integer pageNum = Integer.parseInt(params.get("pageNum"));//获取pageNum
		Integer pageSize = Integer.parseInt(params.get("pageSize"));//获取pageSize
		StringBuffer sql=new StringBuffer();
		PageInfo<Movies> pageInfo=null;
		if (params.get("typeName")!=null) {

			sql.append("SELECT movie_id,movie_name,movie_publish_date");
			sql.append(",movie_intro,movie_link,movie_picture,movie_rating");
			sql.append(",movie_count,movie_duration,movie_status");
			sql.append(" FROM movie_info ");
			sql.append("where movie_type like ?");
			sql.append("ORDER BY movie_publish_date desc limit ? , ?");
			
			rs=DBUtil.doQuery(sql.toString(), "%"+params.get("typeName")+"%",(pageNum-1)*pageSize,pageSize);
			try {
				while(rs.next()) {
					String movieId = rs.getString("movie_id");
					String movieName = rs.getString("movie_name");
					String moviePublishDate = rs.getString("movie_publish_date");
					String movieIntro = rs.getString("movie_intro");
					String movieLink = rs.getString("movie_link");
					String moviePicture = rs.getString("movie_picture");
					String movieRating = rs.getString("movie_rating");
					String movieCount = rs.getString("movie_count");
					String movieDuration = rs.getString("movie_duration");
					String movieStatus = rs.getString("movie_status");
					Movies movies = new Movies(movieId, movieName, moviePublishDate, movieIntro, movieLink, moviePicture, movieRating, null, null, movieCount, movieDuration, movieStatus, null, null, null, null, null, null, null, null);
					list.add(movies);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			Integer total = 0;
			// 获取符合当前查询条件的总记录数
			rs = DBUtil.doQuery("select count(*) FROM movie_info where movie_type like ? ", "%"+params.get("typeName")+"%");
			try {
				while (rs.next()) {
					total = rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			pageInfo=new PageInfo<>(list, pageNum, pageSize, total);
			
			
		}else if (params.get("areaName")!=null) {
			sql.append("SELECT  A.movie_id,A.movie_name,A.movie_publish_date");
			sql.append(",A.movie_intro,A.movie_link,A.movie_picture,A.movie_rating");
			sql.append(",A.movie_count,A.movie_duration,A.movie_status,B.country_id,B.country_name ");
			sql.append(" from movie_info A ");
			sql.append("INNER JOIN country_info B ");
			sql.append("on A.country_id = B.country_id ");
			sql.append("WHERE B.country_name=?");
			sql.append("ORDER BY A.movie_publish_date desc limit ? , ?");
			rs=DBUtil.doQuery(sql.toString(), params.get("areaName"),(pageNum-1)*pageSize,pageSize);
			try {
				while(rs.next()) {
					String movieId = rs.getString("movie_id");
					String movieName = rs.getString("movie_name");
					String moviePublishDate = rs.getString("movie_publish_date");
					String movieIntro = rs.getString("movie_intro");
					String movieLink = rs.getString("movie_link");
					String moviePicture = rs.getString("movie_picture");
					String movieRating = rs.getString("movie_rating");
					String movieCount = rs.getString("movie_count");
					String movieDuration = rs.getString("movie_duration");
					String movieStatus = rs.getString("movie_status");
					String countryId = rs.getString("country_id");
					String countryName = rs.getString("country_name");
					Country country=new Country(countryId, countryName, null);
					Movies movies = new Movies(movieId, movieName, moviePublishDate, movieIntro, movieLink, moviePicture, movieRating, null, countryId, movieCount, movieDuration, movieStatus, null, countryName, null, null, null, country, null, null);
					list.add(movies);
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			Integer total = 0;
			// 获取符合当前查询条件的总记录数
			rs = DBUtil.doQuery("SELECT  COUNT(*) " + 
					"  from movie_info A INNER JOIN country_info B " + 
					"  on A.country_id = B.country_id " + 
					"  WHERE B.country_name=?", params.get("areaName"));
			try {
				while (rs.next()) {
					total = rs.getInt(1);
				}
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			pageInfo=new PageInfo<>(list, pageNum, pageSize, total);
			
		
		}
		
		
	
		return pageInfo;
	}

	/* 通过评分和点击量发布时间来排行查询
	 * @see com.mvetc.dao.MoviesDao#getListMoviesByQualityChoiceBycf(java.util.Map)
	 */
	@Override
	public List<Movies> getListMoviesByQualityChoiceBycf(Map<String, String> params) {
		Map<String, String> map=new HashMap<String, String>();
		List<Movies> list=new ArrayList<Movies>();
		ResultSet rs=null;
		StringBuffer sql=new StringBuffer();
		String score = params.get("score");//评分
		String clickCount = params.get("clickCount");//点击量
		 String publickDate = params.get("publickDate");//发布时间
		sql.append("SELECT  A.movie_id,A.movie_name,A.movie_publish_date,A.movie_intro");  
		sql.append(",A.movie_link,A.movie_picture,A.movie_rating,A.movie_count,A.movie_duration");
		sql.append(",A.movie_status,B.country_id,B.country_name ");
		sql.append("from movie_info A ");
		sql.append("INNER JOIN country_info B ");
		sql.append("on A.country_id = B.country_id  order by ");
		if(score!=null&&clickCount!=null) {//先按评分后按点击量
			sql.append("A.movie_rating desc , A.movie_count desc");		
		}else if (score==null&&clickCount!=null) {//按点击量
			sql.append("A.movie_count desc");
		}else if (score!=null&&clickCount==null) {//按评分
			sql.append("A.movie_rating desc");
		}else if (publickDate!=null&&score==null&&clickCount==null) {
			
			sql.append("A.movie_publish_date desc");
		}
		rs=DBUtil.doQuery(sql.toString());
		try {
			while(rs.next()) {
				String movieId = rs.getString("movie_id");
				String movieName = rs.getString("movie_name");
				String moviePublishDate = rs.getString("movie_publish_date");
				String movieIntro = rs.getString("movie_intro");
				String movieLink = rs.getString("movie_link");
				String moviePicture = rs.getString("movie_picture");
				String movieRating = rs.getString("movie_rating");
				String movieCount = rs.getString("movie_count");
				String movieDuration = rs.getString("movie_duration");
				String movieStatus = rs.getString("movie_status");
				String countryId = rs.getString("country_id");
				String countryName = rs.getString("country_name");
				Country country=new Country(countryId, countryName, null);
				Movies movies = new Movies(movieId, movieName, moviePublishDate, movieIntro, movieLink, moviePicture, movieRating, null, countryId, movieCount, movieDuration, movieStatus, null, countryName, null, null, null, country, null, null);
				list.add(movies);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		
		return list;
	}

}
